using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace View {
    public class BallView {

        private GameCamera cam;
        private Model.BallSimulation ballSim;

        private Texture2D ballTexture;
        private Texture2D wallTexture;

        private Vector2 startPos;
        private Vector2 ballPos;
        private Vector2 ballSize;
        private Vector2 ballOffset;
        private Vector2 gameArea;
        private Vector2 borderSize;

        private List<ShootParticleSystem> shootSystems = new List<ShootParticleSystem>();
        private List<Model.Ball> ballList = new List<Model.Ball>();

        public BallView(Model.BallSimulation ballSim) 
        {
            this.ballSim = ballSim;

            cam = new GameCamera(new Vector2(ballSim.GetBorderSize, ballSim.GetBorderSize), ballSim.GetLevelWidth, ballSim.GetLevelHeight);
            //cam.fitGameToScreenAsSquare();

            // Load in all textures.
            ballTexture = Resources.Load<Texture2D>("Textures/BallTexture");
            wallTexture = Resources.Load<Texture2D>("Textures/WallTexture");

            // Setup game level.

            ballSize = cam.getVisualSizeWithRadius(0.025f);
            gameArea = cam.getVisualSize(ballSim.GetLevelWidth, ballSim.GetLevelHeight);
            borderSize = cam.getVisualSize(ballSim.GetBorderSize, ballSim.GetBorderSize);

            //shoot = new ShootParticleSystem(new Vector2(0.5f, 0.5f), 0.2f);
        }

        /// <summary>
        /// Call on this in MasterController.Update() to run view logic.
        /// </summary>
	    public void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                Vector2 mousePos = cam.getLogicalCoordinates(new Vector2(Input.mousePosition.x, 
                                                                        (Input.mousePosition.y - Screen.height) * -1));
                shootSystems.Add(new ShootParticleSystem(new Vector2(mousePos.x, mousePos.y), 0.2f));
            }

            foreach (var ball in ballSim.GetBallList)
            {
                ball.visualPosition = cam.getVisualCoordinates(ball.Position);
            }
            
            foreach (ShootParticleSystem shoot in shootSystems)
            {
                shoot.Update();
            }
	    }

        /// <summary>
        /// Call on this in MasterController.OnGUI() to run view logic.
        /// </summary>
        public void OnGUI()
        {
            foreach (var ball in ballSim.GetBallList)
            {
                drawBall(ball.visualPosition);
            }
            
            drawWall();

            foreach (ShootParticleSystem shoot in shootSystems)
            {
                shoot.OnGUI(cam);
            }
        }

        public void drawBall(Vector2 ballPos)
        {
            GUI.DrawTexture(new Rect(ballPos.x - ballSize.x / 2, ballPos.y - ballSize.y / 2, ballSize.x, ballSize.y), ballTexture);
        }

        public void drawWall() 
        {
            GUI.skin.box.normal.background = wallTexture;
            GUI.skin.box.border = new RectOffset(4, 4, 4, 4);
            GUI.Box(new Rect(borderSize.x, borderSize.y, gameArea.x - borderSize.x * 2, gameArea.y - borderSize.y * 2), "");
        }
    }
}